package com.es.spring_es.rabbitmq.mq;

import com.es.spring_es.rabbitmq.msg.MqMsg;
import com.es.spring_es.services.EsServices;
import com.rabbitmq.client.Channel;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;

import java.util.Map;


/**
 * Created with IntelliJ IDEA
 * User: xu
 * DATE: 2019/11/12
 * TIME: 2019-11-12
 * Description: 消费者,队列要事先存在
 */
@Configuration
public class Consume {

    @Autowired
    EsServices esServices;

    private static org.apache.logging.log4j.Logger logger = LogManager.getLogger(Consume.class);

    @RabbitHandler
    @RabbitListener(queues = "track", containerFactory = "shanghaiNACK")
    public void completeOrder(@Payload MqMsg msg, Channel channel, @Headers Map<String, Object> headers, @Header(AmqpHeaders.DELIVERY_TAG) long tag) {
        try {
            net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(msg);
            String trid = json.getJSONObject("data").getString("trid");
            String tid = json.getJSONObject("data").getString("tid");

            esServices.uploadDriverLocation(tid,Long.parseLong(trid));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}